﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using PNProject.Core;
using PNProject.Core.Converter;
using PNProject.Models;
using PNProject.EF;
using System.Globalization;

namespace PNProject.Controllers
{
    public class LibrariesController : Controller
    {
        //
        // GET: /Libraries/

        public ActionResult Index(string p)
        {
            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            ViewData["lang"] = lang;
            int page = PNLibraries.PageVerify(p);
            var list = StaticData.libRepo.GetSome(a => a.Lang == lang).ToList();
            var model = list.Skip((page - 1) * 10).Take(10).ToList();
            ViewData["libs"] = model;
            var count = list.Count;
            int mod = count % 10;
            int pc = mod == 0 ? count / 10 : (count / 10) + 1;
            ViewData["page-pc"] = pc;
            ViewData["page-total"] = count;
            ViewData["page-active"] = page;
            ViewData["page-list"] = PNLibraries.GetShowedPage(count, page, 10, 5);
            return View();
        }

        public ActionResult Detail(string id)
        {
            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            ViewData["lang"] = lang;
            var dbo = StaticData.libRepo.GetOne(a => a.Lang == lang && a.TLink==id);
            if (dbo == null)
                return RedirectToAction("Error", "Home");
            ViewData["lib"] = dbo;
            var list = StaticData.libRepo.GetSome(a => a.Lang == lang && a.TLink!=id).ToList();
            ViewData["relative"] = list;
            return View();
        }

        public ActionResult Search(string query, string p)
        {
            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            ViewData["lang"] = lang;
            int page = PNLibraries.PageVerify(p);
            var list = StaticData.libRepo.GetSome(a => a.Lang == lang && a.Title.Contains(query)).ToList();
            var model = list.Skip((page - 1) * 10).Take(10).ToList();
            ViewData["news"] = model;
            var count = list.Count;
            int mod = count % 8;
            int pc = mod == 0 ? count / 10 : (count / 10) + 1;
            ViewData["query"] = query;
            ViewData["page-pc"] = pc;
            ViewData["page-total"] = count;
            ViewData["page-active"] = page;
            ViewData["page-list"] = PNLibraries.GetShowedPage(count, page, 10, 5);
            return View();
        }

        public ActionResult GoLink(string id)
        {
            Guid newid=Guid.NewGuid();
            try{
                newid = Guid.Parse(id);
            }catch(Exception e){}
            var dbo = StaticData.libRepo.GetOne(a => a.ElementID == newid);
            if (dbo != null)
            {
                var copy = LibConverter.CopyLib(dbo);
                int d = dbo.ViewCount;
                d++;
                copy.ViewCount = d;
                StaticData.libRepo.Save(dbo, copy);
                return Redirect(dbo.URL);
            }
            return RedirectToAction("error", "home");

        }

    }
}
